/*
 * DCE400服务系统 -- 后端服务接口 -- DAO 标签
 * 包括：
 */
package dao

import (
	"errors"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/db"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/util"
	"go.uber.org/zap"
	"strings"
	"time"
)

//********************************************
//Func  ：查询标签列表，分页查询。
//Method：
//Usage ：
//********************************************
func QueryLabelList(page, pageSize int) (int64, []*entity.Label, error) {
	offset := (page - 1) * pageSize
	session := db.Session().Table("label")
	defer session.Close()

	if pageSize > 0 {
		session.Limit(pageSize, offset)
	}
	labels := []*entity.Label{}

	total, err := session.FindAndCount(&labels)
	if err != nil {
		return 0, nil, err
	}
	return total, labels, nil
}

//********************************************
//Func  ：查询数据库：指定工单的详情
//Method：GET
//Usage ：
//********************************************
func QueryLabelDetail(Id int64) (*entity.Label, error) {
	session := db.Session().Table("label") //.Where("id=?",operatorId).Get(&operator)
	defer session.Close()

	var label entity.Label
	has, err := session.Where("id=?", Id).Get(&label)
	if err != nil || has == false {
		return nil, err
	}
	return &label, nil
}

//********************************************
//Func  ：添加新标签
//Method：POST
//Usage ：
//********************************************
func CreateLabel(label entity.Label) (*entity.Label, error) {
	var err error
	if len(strings.TrimSpace(label.Label)) <= 0 { //姓名（必填）/单位名称
		return nil, errors.New("name is none")
	}

	label.CreatedAt = time.Now()
	label.Id = util.GenSnowflakeInt64()
	global.Log.Info("InsertLabel: ", zap.Any("label:", label))

	session := db.Session().Table("label")
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return nil, errors.New("db session begin failed.")
	}

	_, err = session.Insert(&label) //插入
	if err != nil {
		session.Rollback()
		return nil, err
	}
	err = session.Commit()
	if err != nil {
		return nil, err
	}

	return &label, nil
}

//********************************************
//Func  ：更新信息标签
//Usage ：
//********************************************
func UpdateLabel(label entity.Label) error {
	var err error
	if len(strings.TrimSpace(label.Label)) <= 0 {
		return errors.New("label is none")
	}
	if label.Id <= 0 {
		return errors.New("label id is none")
	}

	session := db.Session().Table("label")
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return err
	}

	_, err = session.Where("id=?", label.Label).
		Cols("Label,remarks").Update(label) //更新

	if err != nil {
		return err
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}

//********************************************
//Func  ：删除标签
//Usage ：标签-删除
//********************************************
func DeleteLabel(id int64) error {
	if id <= 0 {
		return errors.New("未指定标签ID信息")
	}
	session := db.Session().Table("label")
	defer session.Close()
	session = session.Where("id=?", id)
	session.Begin()
	cnt, err := session.Delete(&entity.Label{})
	if err != nil {
		session.Rollback()
		return err
	}
	if cnt <= 0 {
		session.Rollback()
		return errors.New("移除标签数量0")
	}
	err = session.Commit()
	return err
}

//********************************************
//Func  ：添加标签-工单表
//Usage ：
//********************************************
func CreateLabelOrderObj(labelId int64, orderId int64) error {
	var obj entity.WorkOrderLabel
	obj.CreatedAt = time.Now()
	obj.Id = util.GenSnowflakeInt64()
	obj.LabelId = labelId
	obj.WorkOrderId = orderId
	global.Log.Info("CreateLabelOrderObj: ", zap.Any("WorkOrderLabel:", obj))

	count, err := db.Session().Table("work_order_label").
		Where("label_id=? and order_id=?", labelId, orderId).Count(&entity.WorkOrderLabel{})
	if count > 0 {
		return errors.New("label has exist in this order.")
	}

	session := db.Session().Table("work_order_label")
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return errors.New("db session begin failed.")
	}
	_, err = session.Insert(&obj) //插入
	if err != nil {
		session.Rollback()
		return err
	}

	err = session.Commit()
	return err
}

//********************************************
//Func  ：移除标签-工单表
//Usage ：
//********************************************
func DeleteLabelOrderObj(labelId int64, orderId int64) error {
	var err error
	var obj entity.WorkOrderLabel
	global.Log.Info("DeleteLabelOrderObj: ", zap.Any("WorkOrderLabel:", obj))

	session := db.Session().Table("work_order_label")
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return errors.New("db session begin failed.")
	}
	_, err = session.Where("label_id=? and work_order_id=?", labelId, orderId).Delete(&obj) //del
	if err != nil {
		session.Rollback()
		return err
	}
	err = session.Commit()
	return err
}
